package com.xiaoleng.tlgsku.controller;


import com.xiaoleng.tlgsku.pojo.AttributeKey;
import com.xiaoleng.tlgsku.pojo.AttributeValue;
import com.xiaoleng.tlgsku.pojo.GoodsSpecs;
import com.xiaoleng.tlgsku.service.AttributeKeyService;
import com.xiaoleng.tlgsku.service.AttributeValueService;
import com.xiaoleng.tlgsku.service.GoodsSpecsService;
import com.xiaoleng.tlgsku.tools.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping("/sku")
public class GoodsSpecsController {


    @Autowired
    private AttributeKeyService attributeKeyService;
    @Autowired
    private AttributeValueService attributeValueService;
    @Autowired
    private GoodsSpecsService goodsSpecsService;

    /**
     * 根据商品id查询规格
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/findValuekeyBygoodsId")
    public Record findValuekeyBygoodsId(Integer goodsId) {
        return attributeKeyService.findValuekeyBygoodsId(goodsId);
    }

    /**
     * 添加规格值
     *
     * @param attributeValue
     * @param attributeKey
     */
    @PostMapping("/addValue")
    public Record addValue(AttributeValue attributeValue, AttributeKey attributeKey) {
        attributeKeyService.addKey(attributeKey);
        Record record = attributeKeyService.findNameBykeyId(attributeKey.getAttributeName());
        attributeValue.setAttributeId((Integer) record.getData());
        return attributeValueService.addValue(attributeValue);
    }

    /**
     * 根据keyid查询规则值
     *
     * @param attributeId
     * @return
     */
    @GetMapping("/findValueByKeyId")
    public Record findValueByKeyId(Integer attributeId) {
        return attributeValueService.findValueByKeyId(attributeId);
    }

    /**
     * 添加规格key
     *
     * @param attributeKey
     */
    @PostMapping("/addKey")
    public Record addKey(AttributeKey attributeKey) {
        return attributeKeyService.addKey(attributeKey);
    }

    /**
     * 删除规格
     *
     * @param goodsId
     */
    @DeleteMapping("/delete")
    public Record delete(Integer goodsId) {
        return attributeValueService.delete(goodsId);
    }

    /**
     * 根据分类id查询key
     *
     * @param typeId
     */
    @GetMapping("/findkeyBytypeId")
    public Record findkeyBytypeId(Integer typeId) {
        return attributeKeyService.findkeyBytypeId(typeId);
    }

//    @PutMapping("/modidy")
//    public void modidy(String attributeValue,Integer id){
//        attributeValueService.modidy(attributeValue,id);
//    }

    /**
     * 根据商品id和规格id查询规格值
     *
     * @param valueId
     * @param goodsId
     * @return
     */
    @GetMapping("/findimgBygoodsId")
    public Record findimgBygoodsId(Integer valueId, Integer goodsId) {
        return goodsSpecsService.findimgBygoodsId(valueId, goodsId);
    }

    /**
     * 添加规格信息
     *
     * @param goodsSpecs
     * @return
     */
    @PostMapping("/addSpecs")
    public Record addSpecs(GoodsSpecs goodsSpecs) {
        return goodsSpecsService.addSpecs(goodsSpecs);
    }

    /**
     * 根据规格list中的keyid 和 keyvalue查 规格集合
     *
     * @param keyId
     * @param keyValueId
     * @param valueId
     * @return
     */
    @GetMapping("/findlist")
    public Record findlist(Integer keyId, Integer keyValueId, Integer valueId, Integer specsId) {
        return goodsSpecsService.findlist(keyId, keyValueId, valueId, specsId);
    }

    /**
     * 规格详情和库存集合
     *
     * @param goodsId
     * @param keyId
     * @param keyValueId
     * @param valueId
     * @return
     */
    @GetMapping("/info")
    public Record info(Integer goodsId, Integer keyId, Integer keyValueId, Integer valueId, Integer specsId) {
        return goodsSpecsService.info(goodsId, keyId, keyValueId, valueId, specsId);
    }


    /**
     * 查询规格集合
     *
     * @param goodsId     商品id
     * @param keyId       规格所属id  例如: 颜色 1
     * @param attributeId 规格id  例如：黑色 2
     * @return
     */
    @GetMapping("/getSpecsList")
    public Record findSpecsListByGoodsId(Integer goodsId, Integer keyId, Integer attributeId) {
        return attributeKeyService.findSpecsListByGoodsId(goodsId, keyId, attributeId);
    }


    /**
     * 查询规格信息
     *
     * @param goodsId   商品id
     * @param specsList 选中的规格
     * @return
     */
    @GetMapping("/getSpecsInfo")
    public Record findSpecsInfo(Integer goodsId, String specsList) {
        return goodsSpecsService.findSpecsInfo(goodsId, specsList);
    }
}
